<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      label-width="120px"
      style="width: 650px"
      :rules="rules"
    >
      <el-form-item label="教练名称" prop="coach_name">
        <el-input
          type="text"
          v-model="form.coach_name"
          style="width: 150px"
        ></el-input>
      </el-form-item>
      <el-form-item label="教练照片" prop="coach_cover">
        <el-upload
          class="avatar-uploader"
          :action="`${UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :limit="1"
        >
          <img v-if="form.coach_cover" :src="form.coach_cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="负责科目" prop="subjectstype">
        <el-checkbox-group v-model="form.subjectstype">
          <el-checkbox
            v-for="item in subjectstypes"
            :key="item.subjects_id"
            :label="item.subjects_type"
            :value="item.subjects_type"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="教授驾照类型" prop="licensetype">
        <el-checkbox-group v-model="form.licensetype">
          <el-checkbox
            v-for="item in licensetypes"
            :key="item.license_type_id"
            :label="item.license_type"
            :value="item.license_type"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="所属驾校" prop="schoolname">
        <el-input type="text" v-model="form.schoolname"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="coach_phone">
        <el-input type="text" v-model="form.coach_phone"></el-input>
      </el-form-item>
      <el-form-item label="所在城市" prop="city_name" style="width: 330px">
        <el-input type="text" v-model="form.city_name"></el-input>
      </el-form-item>
      <!-- 教练标签 -->
      <el-form-item label="教练标签" prop="coachlabel">
        <el-select
          v-model="form.coachlabel"
          multiple
          placeholder="请选择教练标签"
        >
          <el-option
            v-for="item in coach_labels"
            :key="item.id"
            :label="item.coach_label"
            :value="item.coach_label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="教练评分"
        prop="coach_score"
        placeholder="请输入评分0-5"
      >
        <el-input
          type="text"
          v-model="form.coach_score"
          style="width: 210px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update">修改教练</el-button>
        <el-button @click="resetform">重置</el-button>
        <el-button @click="cancel">取消返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http";
export default {
  // watch: {
  //   form: {
  //     deep: true,
  //     handler(val) {
  //       console.log("from改变了", val);
  //     },
  //   },
  // },
  data() {
    return {
      form: {
        coach_id: this.$route.params.id,
        coach_name: "",
        // 教练照片
        coach_cover: "",
        subjectstype: [],
        licensetype: [],
        schoolname: "",
        city_name: "",
        coach_phone: "",
        coach_score: "",
        coachlabel: [],
      },
      // 教练标签
      coach_labels: [],
      ciyts: [],
      licensetypes: [],
      subjectstypes: [],
      rules: {
        coach_name: [
          { required: true, message: "请输入教练名称", trigger: "blur" },
        ],

        coach_cover: [
          {
            required: true,
            message: "请上传图片",
            trigger: "blur",
          },
        ],

        subjectstype: [
          {
            required: true,
            message: "请选择科目类型",
            trigger: "blur",
          },
        ],

        licensetype: [
          {
            required: true,
            message: "请选择驾照类型",
            trigger: "blur",
          },
        ],

        schoolname: [
          {
            required: true,
            message: "请输入驾校名称",
            trigger: "blur",
          },
        ],

        city_name: [
          {
            required: true,
            message: "请输城市名称",
            trigger: "blur",
          },
        ],

        coach_phone: [
          {
            required: true,
            message: "请输入电话号码",
            trigger: "blur",
          },
          // {
          //   message: "手机号格式错误",
          //   trigger: "blur",
          //   pattern: /^1[3456789]d{9}$/,
          // },
        ],

        coach_score: [
          {
            required: true,
            message: "请输入驾校名称",
            trigger: "blur",
          },
        ],
        coachlabel: [
          {
            required: true,
            message: "请输入驾校名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetform() {
      this.form.coach_name = "";
      this.form.coach_phone = "";
      this.form.coach_score = "";
      this.form.coach_cover = "";
      this.form.subjectstype = [];
      this.form.schoolname = "";
      this.form.licensetype = [];
      this.form.coachlabel = "";
      this.form.city_name = "";
    },
    cancel() {
      this.$router.push("/home/coach-list");
    },
    update() {
      this.form.licensetype = this.form.licensetype.join("/");
      this.form.coachlabel = this.form.coachlabel.join("/");
      this.form.subjectstype = this.form.subjectstype.join("/");
      this.$refs["form"].validate((valid) => {
        if (valid) {
          httpApi.coachApi.update(this.form).then((res) => {
            console.log("教练修改", res);
            if (res.data.code == 200) {
              this.$message.success("教练修改成功");
              this.$router.push("/home/coach-list");
            } else {
              this.$message.error("添加失败请检查");
              this.form.licensetype = this.form.licensetype.split("/");
              this.form.coachlabel = this.form.coachlabel.split("/");
              this.form.subjectstype = this.form.subjectstype.split("/");
            }
          });
        }
      });
    },
    // 图片上传处理
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.form.coach_cover = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传驾校照片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传驾校照片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    infoCoachlab() {
      httpApi.coachApi.querytype().then((res) => {
        console.log("加载教练标签", res);
        this.coach_labels = res.data.data.result;
      });
    },
    infocity() {
      httpApi.commpleApi.queryallCity().then((res) => {
        console.log("加载城市标签", res);
      });
    },
    infolicensetype() {
      httpApi.commpleApi.queryLicensetype().then((res) => {
        console.log("驾照类型标签", res);
        this.licensetypes = res.data?.data?.result;
      });
    },
    infosubjiettype() {
      httpApi.commpleApi.querySubjectstype().then((res) => {
        console.log("科目类型标签", res);
        this.subjectstypes = res.data.data.result;
      });
    },
    queryCaochId() {
      let params = {
        coach_id: this.form.coach_id,
      };
      // console.log(params);
      httpApi.coachApi.queryById(params).then((res) => {
        console.log("加载教练信息", res);
        this.form = res.data.data.result[0];
        this.form.coachlabel = this.form.coachlabel.split("/");
        this.form.subjectstype = this.form.subjectstype.split("/");
        this.form.licensetype = this.form.licensetype.split("/");
      });
    },
  },
  mounted() {
    this.infoCoachlab();
    this.infocity();
    this.infolicensetype();
    this.infosubjiettype();
    this.queryCaochId();
  },
};
</script>

<style lang="scss" scoped></style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;

  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>
